home *** CD-ROM | disk | FTP | other *** search
/ United Public Domain Gold 2 / United Public Domain Gold 2.iso / utilities / pu248.dms / pu248.adf / Intuition / Menus / Example5.c < prev    next >
C/C++ Source or Header  |  1992-05-01  |  17KB  |  425 lines

  1. /***********************************************************/
  2. /*                                                         */
  3. /* Amiga C Encyclopedia (ACE) V3.0      Amiga C Club (ACC) */
  4. /* -------------------------------      ------------------ */
  5. /*                                                         */
  6. /* Book:    ACM Intuition               Amiga C Club       */
  7. /* Chapter: Menus                       Tulevagen 22       */
  8. /* File:    Example5.c                  181 41  LIDINGO    */
  9. /* Author:  Anders Bjerin               SWEDEN             */
  10. /* Date:    92-05-01                                       */
  11. /* Version: 1.10                                           */
  12. /*                                                         */
  13. /*   Copyright 1992, Anders Bjerin - Amiga C Club (ACC)    */
  14. /*                                                         */
  15. /* Registered members may use this program freely in their */
  16. /*     own commercial/noncommercial programs/articles.     */
  17. /*                                                         */
  18. /***********************************************************/
  19.  
  20. /* This program opens a normal window to which we connect a menu strip. */
  21. /* The menu will look like this:                                        */
  22. /*                                                                      */
  23. /* Mode                                                                 */
  24. /* ------------------                                                   */
  25. /* | -> Plain       |                                                   */
  26. /* |    Bold        |                                                   */
  27. /* |    Underlined  |                                                   */
  28. /* |    Italic      |                                                   */
  29. /* ------------------                                                   */
  30. /*                                                                      */
  31. /* Exactly as Example1 except that we have changed Intuition's check-   */
  32. /* mark to our own customized "arrow". If you want to use your own      */
  33. /* image instead of Intuition's default one you need to:                */
  34. /* 1. Declare and initialize an Image structure with your requirements. */
  35. /* 2. Set the CheckMark field in the NewWindow structure to point at    */
  36. /*    your Image.                                                       */
  37.  
  38.  
  39.  
  40. #include <intuition/intuition.h>
  41.  
  42.  
  43.  
  44. struct IntuitionBase *IntuitionBase;
  45.  
  46.  
  47.  
  48. /*************************************************************************/
  49. /*                           T H E   S P O T                             */
  50. /*************************************************************************/
  51.  
  52. /* Data for the spot: */
  53. USHORT chip spot_data[]=
  54. {
  55.   0x0300,  /* 0000 0011 0000   0 : White   */
  56.   0x0180,  /* 0000 0001 1000   1 : Orange  */
  57.   0x00C0,  /* 0000 0000 1100               */
  58.   0x0060,  /* 0000 0000 0110               */
  59.   0xFFF0,  /* 1111 1111 1111               */
  60.   0xFFF0,  /* 1111 1111 1111               */
  61.   0x0060,  /* 0000 0000 0110               */
  62.   0x00C0,  /* 0000 0000 1100               */
  63.   0x0180,  /* 0000 0001 1000               */
  64.   0x0300,  /* 0000 0011 0000               */
  65. };
  66.  
  67. /* The spot's Image structure: */
  68. struct Image spot=
  69. {
  70.   0,         /* LeftEdge, 0 pixels out. */
  71.   -1,        /* TopEdge, 1 line up. */
  72.   12,        /* Width, 12 pixels wide. */
  73.   10,        /* Height, 10 lines heigh. */
  74.   1,         /* Depth, one Bitplane. */
  75.   spot_data, /* ImageData, pointer to the image data. */
  76.   0x2,       /* PlanePick, affect the Bitplane one. */
  77.   0x1,       /* PlaneOnOff, fill Bitplane zero with 1's. */
  78.   NULL,      /* NextImage, no Image structure connected to this one. */
  79. };
  80.  
  81.  
  82.  
  83. /*************************************************************************/
  84. /*                         F O U R T H   I T E M                         */
  85. /*************************************************************************/
  86.  
  87. /* The text for the fourth item: */
  88. struct IntuiText my_fourth_text=
  89. {
  90.   2,            /* FrontPen, black. */
  91.   0,            /* BackPen, not used since JAM1. */
  92.   JAM1,         /* DrawMode, do not change the background. */
  93.   CHECKWIDTH,   /* LeftEdge, CHECKWIDTH amount of pixels out. */
  94.                 /* This will leave enough space for the check mark. */
  95.   1,            /* TopEdge, 1 line down. */
  96.   NULL,         /* TextAttr, default font. */
  97.   "Italic",     /* IText, the string. */
  98.   NULL          /* NextItem, no link to other IntuiText structures. */
  99. };
  100.  
  101. /* The MenuItem structure for the fourth item: */
  102. struct MenuItem my_fourth_item=
  103. {
  104.   NULL,            /* NextItem, this is the last item in the list. */
  105.   0,               /* LeftEdge, 0 pixels out. */
  106.   30,              /* TopEdge, 30 lines down. */
  107.   150,             /* Width, 150 pixels wide. */
  108.   10,              /* Height, 10 lines high. */
  109.   ITEMTEXT|        /* Flags, render this item with text. */
  110.   ITEMENABLED|     /*        this item will be enabled. */
  111.   CHECKIT|         /*        it is an attribute item. */
  112.   HIGHCOMP,        /*        complement the colours when highlihted. */
  113.   0x00000001,      /* MutualExclude, mutualexclude the first item only. */
  114.   (APTR) &my_fourth_text, /* ItemFill, pointer to the text. */
  115.   NULL,            /* SelectFill, nothing since we complement the col. */
  116.   0,               /* Command, no command-key sequence. */
  117.   NULL,            /* SubItem, no subitem list. */
  118.   MENUNULL,        /* NextSelect, no items selected. */
  119. };
  120.  
  121.  
  122.  
  123. /*************************************************************************/
  124. /*                          T H I R D   I T E M                          */
  125. /*************************************************************************/
  126.  
  127. /* The text for the third item: */
  128. struct IntuiText my_third_text=
  129. {
  130.   2,            /* FrontPen, black. */
  131.   0,            /* BackPen, not used since JAM1. */
  132.   JAM1,         /* DrawMode, do not change the background. */
  133.   CHECKWIDTH,   /* LeftEdge, CHECKWIDTH amount of pixels out. */
  134.                 /* This will leave enough space for the check mark. */
  135.   1,            /* TopEdge, 1 line down. */
  136.   NULL,         /* TextAttr, default font. */
  137.   "Underlined", /* IText, the string. */
  138.   NULL          /* NextItem, no link to other IntuiText structures. */
  139. };
  140.  
  141. /* The MenuItem structure for the third item: */
  142. struct MenuItem my_third_item=
  143. {
  144.   &my_fourth_item, /* NextItem, linked to the fourth item. */
  145.   0,               /* LeftEdge, 0 pixels out. */
  146.   20,              /* TopEdge, 20 lines down. */
  147.   150,             /* Width, 150 pixels wide. */
  148.   10,              /* Height, 10 lines high. */
  149.   ITEMTEXT|        /* Flags, render this item with text. */
  150.   ITEMENABLED|     /*        this item will be enabled. */
  151.   CHECKIT|         /*        it is an attribute item. */
  152.   HIGHCOMP,        /*        complement the colours when highlihted. */
  153.   0x00000001,      /* MutualExclude, mutualexclude the first item only. */
  154.   (APTR) &my_third_text, /* ItemFill, pointer to the text. */
  155.   NULL,            /* SelectFill, nothing since we complement the col. */
  156.   0,               /* Command, no command-key sequence. */
  157.   NULL,            /* SubItem, no subitem list. */
  158.   MENUNULL,        /* NextSelect, no items selected. */
  159. };
  160.  
  161.  
  162.  
  163. /*************************************************************************/
  164. /*                         S E C O N D   I T E M                         */
  165. /*************************************************************************/
  166.  
  167. /* The text for the second item: */
  168. struct IntuiText my_second_text=
  169. {
  170.   2,          /* FrontPen, black. */
  171.   0,          /* BackPen, not used since JAM1. */
  172.   JAM1,       /* DrawMode, do not change the background. */
  173.   CHECKWIDTH, /* LeftEdge, CHECKWIDTH amount of pixels out. */
  174.               /* This will leave enough space for the check mark. */
  175.   1,          /* TopEdge, 1 line down. */
  176.   NULL,       /* TextAttr, default font. */
  177.   "Bold",     /* IText, the string. */
  178.   NULL        /* NextItem, no link to other IntuiText structures. */
  179. };
  180.  
  181. /* The MenuItem structure for the second item: */
  182. struct MenuItem my_second_item=
  183. {
  184.   &my_third_item,  /* NextItem, linked to the third item. */
  185.   0,               /* LeftEdge, 0 pixels out. */
  186.   10,              /* TopEdge, 10 lines down. */
  187.   150,             /* Width, 150 pixels wide. */
  188.   10,              /* Height, 10 lines high. */
  189.   ITEMTEXT|        /* Flags, render this item with text. */
  190.   ITEMENABLED|     /*        this item will be enabled. */
  191.   CHECKIT|         /*        it is an attribute item. */
  192.   HIGHCOMP,        /*        complement the colours when highlihted. */
  193.   0x00000001,      /* MutualExclude, mutualexclude the first item only. */
  194.   (APTR) &my_second_text, /* ItemFill, pointer to the text. */
  195.   NULL,            /* SelectFill, nothing since we complement the col. */
  196.   0,               /* Command, no command-key sequence. */
  197.   NULL,            /* SubItem, no subitem list. */
  198.   MENUNULL,        /* NextSelect, no items selected. */
  199. };
  200.  
  201.  
  202.  
  203. /*************************************************************************/
  204. /*                          F I R S T   I T E M                          */
  205. /*************************************************************************/
  206.  
  207. /* The text for the first item: */
  208. struct IntuiText my_first_text=
  209. {
  210.   2,          /* FrontPen, black. */
  211.   0,          /* BackPen, not used since JAM1. */
  212.   JAM1,       /* DrawMode, do not change the background. */
  213.   CHECKWIDTH, /* LeftEdge, CHECKWIDTH amount of pixels out. */
  214.               /* This will leave enough space for the check mark. */
  215.   1,          /* TopEdge, 1 line down. */
  216.   NULL,       /* TextAttr, default font. */
  217.   "Plain",    /* IText, the string. */
  218.   NULL        /* NextItem, no link to other IntuiText structures. */
  219. };
  220.  
  221. /* The MenuItem structure for the first item: */
  222. struct MenuItem my_first_item=
  223. {
  224.   &my_second_item, /* NextItem, linked to the second item. */
  225.   0,               /* LeftEdge, 0 pixels out. */
  226.   0,               /* TopEdge, 0 lines down. */
  227.   150,             /* Width, 150 pixels wide. */
  228.   10,              /* Height, 10 lines high. */
  229.   ITEMTEXT|        /* Flags, render this item with text. */
  230.   ITEMENABLED|     /*        this item will be enabled. */
  231.   CHECKIT|         /*        it is an attribute item. */
  232.   CHECKED|         /*        this item is initially selected. */
  233.   HIGHCOMP,        /*        complement the colours when highlihted. */
  234.   0xFFFFFFFE,      /* MutualExclude, mutualexclude all items except the */
  235.                    /*                first one. */
  236.   (APTR) &my_first_text, /* ItemFill, pointer to the text. */
  237.   NULL,            /* SelectFill, nothing since we complement the col. */
  238.   0,               /* Command, no command-key sequence. */
  239.   NULL,            /* SubItem, no subitem list. */
  240.   MENUNULL,        /* NextSelect, no items selected. */
  241. };
  242.  
  243.  
  244.  
  245. /*************************************************************************/
  246. /*                              M E N U                                  */
  247. /*************************************************************************/
  248.  
  249. /* The Menu structure for the first (and only) menu: */
  250. struct Menu my_menu=
  251. {
  252.   NULL,          /* NextMenu, no more menu structures. */
  253.   0,             /* LeftEdge, left corner. */
  254.   0,             /* TopEdge, for the moment ignored by Intuition. */
  255.   50,            /* Width, 50 pixels wide. */
  256.   0,             /* Height, for the moment ignored by Intuition. */
  257.   MENUENABLED,   /* Flags, this menu will be enabled. */
  258.   "Mode",        /* MenuName, the string. */
  259.   &my_first_item /* FirstItem, pointer to the first item in the list. */
  260. };
  261.  
  262.  
  263.  
  264. /* Declare a pointer to a Window structure: */ 
  265. struct Window *my_window;
  266.  
  267. /* Declare and initialize your NewWindow structure: */
  268. struct NewWindow my_new_window=
  269. {
  270.   50,            /* LeftEdge    x position of the window. */
  271.   25,            /* TopEdge     y positio of the window. */
  272.   200,           /* Width       200 pixels wide. */
  273.   100,           /* Height      100 lines high. */
  274.   0,             /* DetailPen   Text should be drawn with colour reg. 0 */
  275.   1,             /* BlockPen    Blocks should be drawn with colour reg. 1 */
  276.   CLOSEWINDOW|   /* IDCMPFlags  The window will give us a message if the */
  277.                  /*             user has selected the Close window gad. */
  278.   MENUPICK,
  279.   SMART_REFRESH| /* Flags       Intuition should refresh the window. */
  280.   WINDOWCLOSE|   /*             Close Gadget. */
  281.   WINDOWDRAG|    /*             Drag gadget. */
  282.   WINDOWDEPTH|   /*             Depth arrange Gadgets. */
  283.   WINDOWSIZING|  /*             Sizing Gadget. */
  284.   ACTIVATE,      /*             The window should be Active when opened. */
  285.   NULL,          /* FirstGadget No Custom gadgets. */
  286.  
  287.   &spot,         /* CheckMark   Use our own customized checkmark. */
  288.  
  289.   "Style Editor",/* Title       Title of the window. */
  290.   NULL,          /* Screen      Connected to the Workbench Screen. */
  291.   NULL,          /* BitMap      No Custom BitMap. */
  292.   80,            /* MinWidth    We will not allow the window to become */
  293.   30,            /* MinHeight   smaller than 80 x 30, and not bigger */
  294.   300,           /* MaxWidth    than 300 x 200. */
  295.   200,           /* MaxHeight */
  296.   WBENCHSCREEN   /* Type        Connected to the Workbench Screen. */
  297. };
  298.  
  299.  
  300.  
  301. main()
  302. {
  303.   /* Boolean variable used for the while loop: */
  304.   BOOL close_me;
  305.  
  306.   /* Declare a variable in which we will store the IDCMP flag: */
  307.   ULONG class;
  308.   
  309.   /* If we recieve a MENUPICK event, the Code field of the message */
  310.   /* structure will contain the menu number of the first selected item. */
  311.   /* Declare a variable to store the Code value in, and an extra menu */
  312.   /* number variable: */
  313.   USHORT code, menu_number;
  314.   
  315.   /* Declare a MenuItem pointer: */
  316.   struct MenuItem *item;
  317.   
  318.   /* Declare a pointer to an IntuiMessage structure: */
  319.   struct IntuiMessage *my_message;
  320.  
  321.  
  322.  
  323.   /* Before we can use Intuition we need to open the Intuition Library: */
  324.   IntuitionBase = (struct IntuitionBase *)
  325.     OpenLibrary( "intuition.library", 0 );
  326.   
  327.   if( IntuitionBase == NULL )
  328.     exit(); /* Could NOT open the Intuition Library! */
  329.  
  330.  
  331.  
  332.   /* We will now try to open the window: */
  333.   my_window = (struct Window *) OpenWindow( &my_new_window );
  334.   
  335.   /* Have we opened the window succesfully? */
  336.   if(my_window == NULL)
  337.   {
  338.     /* Could NOT open the Window! */
  339.     
  340.     /* Close the Intuition Library since we have opened it: */
  341.     CloseLibrary( IntuitionBase );
  342.  
  343.     exit();  
  344.   }
  345.  
  346.  
  347.  
  348.   /* We have opened the window, and everything seems to be OK. */
  349.  
  350.  
  351.  
  352.   SetMenuStrip( my_window, &my_menu );
  353.   printf("Menustrip connected to window!\n");
  354.  
  355.  
  356.   close_me = FALSE;
  357.  
  358.   /* Stay in the while loop until the user has selected the Close window */
  359.   /* gadget: */
  360.   while( close_me == FALSE )
  361.   {
  362.     /* Wait until we have recieved a message: */
  363.     Wait( 1 << my_window->UserPort->mp_SigBit );
  364.  
  365.     /* As long as we collect messages sucessfully we stay in the loop: */
  366.     while(my_message=(struct IntuiMessage *) GetMsg( my_window->UserPort ))
  367.     {
  368.       /* After we have collected the message we can read it, and save any */
  369.       /* important values which we maybe want to check later: */
  370.       class = my_message->Class;
  371.       code = my_message->Code;
  372.  
  373.  
  374.       /* After we have read it we reply as fast as possible: */
  375.       /* REMEMBER! Do never try to read a message after you have replied! */
  376.       /* Some other process has maybe changed it. */
  377.       ReplyMsg( my_message );
  378.  
  379.       /* Check which IDCMP flag was sent: */
  380.       if( class == CLOSEWINDOW )
  381.         close_me=TRUE; /* The user selected the Close window gadget! */  
  382.  
  383.       if(class == MENUPICK)
  384.       {
  385.         printf("\nMenu pick!\n");
  386.         menu_number = code;
  387.         
  388.         while( menu_number != MENUNULL )
  389.         {
  390.           /* Get the address of the item: */
  391.           item = (struct MenuItem *) ItemAddress( &my_menu, menu_number );
  392.  
  393.  
  394.           /* Print out the menu number plus etc: */
  395.           printf("menu_number= %d\n", menu_number );
  396.           printf("MENUNUM = %d\n", MENUNUM(menu_number) );
  397.           printf("ITEMNUM = %d\n", ITEMNUM(menu_number) );
  398.           printf("SUBNUM  = %d\n", SUBNUM(menu_number) );
  399.  
  400.  
  401.           /* Get the following item's menu number: */
  402.           menu_number = item->NextSelect;
  403.         }
  404.       }
  405.     }
  406.   }
  407.  
  408.  
  409.  
  410.   printf("Menustrip removed from window!\n");
  411.   ClearMenuStrip( my_window );
  412.  
  413.  
  414.  
  415.   /* Close the window: */
  416.   CloseWindow( my_window );
  417.  
  418.  
  419.  
  420.   /* Close the Intuition Library since we have opened it: */
  421.   CloseLibrary( IntuitionBase );
  422.   
  423.   /* THE END */
  424. }
  425.